import cv2
import numpy as np

if __name__ == '__main__':
    # 1. 图片输入
    image_np = cv2.imread('work4.jpg')
    image_hsv = cv2.cvtColor(image_np, cv2.COLOR_BGR2HSV)
    # _, image_thresh = cv2.threshold(image_gray, 192, 255, cv2.THRESH_BINARY)
    g_min = (35, 43, 46)
    g_max = (77, 255, 255)
    mask = cv2.inRange(image_hsv, g_min, g_max)
    mask = cv2.bitwise_not(mask)
    conunters, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    for cnt in conunters:
        print(cnt.shape)
    for b in range(0, 5):
        image_np = cv2.drawContours(image_np, conunters, contourIdx= b, color=(0, 0, 255), thickness= 2)
    for p in range(5, 7):
        image_np = cv2.drawContours(image_np, conunters, contourIdx= p, color=(255, 0, 0), thickness= 2)

    image_np = cv2.drawContours(image_np, conunters, contourIdx= 7, color=(0, 0, 255), thickness=2)

    image_np = cv2.drawContours(image_np, conunters, contourIdx= 8, color=(255, 0, 0), thickness=2)

    cv2.imshow('', image_np)
    cv2.imwrite('zuoye4.jpg', image_np)
    cv2.waitKey(0)